<?php

namespace teach\controllers;

use teach\components\WeChatPay;
use teach\components\WeChatPayOne;
use teach\service\Recharge;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;

/**
 * 充值业务
 * Class RechargeController
 * @package teach\controllers
 */
class RechargeController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    '*' => ['post'],
                ],
            ],
        ];
    }

    /**
     * 充值数据
     */
    public function actionLists()
    {
        $recharge = new Recharge();
        $rechargeLists = $recharge->rechargeLists();
        if ($rechargeLists) {
            $jsonData = ResponseHelper::buildJsonData(200, '成功', $rechargeLists);
        } else {
            $jsonData = ResponseHelper::buildJsonData(400, '失败');
        }
        $this->asJson($jsonData);
    }

    public function actionPay()
    {
        $request = Yii::$app->request;
        $mid = $request->post('mid'); //如果是商家 就mid 跟uid发一样的过来
        $uid = $request->post('uid');
        $openid = $request->post('openid');
        $phone = $request->post('phone');
        $name = $request->post('name');
        $total_fee = $request->post('total_fee');
        $unionid = $request->post('unionid');

        $msg = '';
        $flag = false;

        if (!$uid) {
            $msg = 'uid 不能为空';
        } elseif (!$mid) {
            $msg = 'mid 不能为空';
        } elseif (!$openid) {
            $msg = 'openid 不能为空';
        } elseif (!$total_fee || ($total_fee <= 0)) {
            $msg = 'total_fee 不能为空';
        } else {
            $flag = true;
        }

        if (!$flag) {
            $jsonData = ResponseHelper::buildJsonData(400, $msg);
        } else {
            $spdill_create_ip = $_SERVER['REMOTE_ADDR'];
            $out_trade_no = 're'. date('Ymd') . time() . str_pad(mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
            //如果是uid = mid 说明是商家 充值到绣学问里，如果是 uid ！= mid 充值到特约商户号里
            if ($uid == $mid) {
                $appid = Yii::$app->params['mini']['wxAppId'];
                $mch_id = Yii::$app->params['mini']['mchId'];
                $key = Yii::$app->params['mini']['md5Key'];
                $body = "代理商充值";
                $notify_url = Yii::$app->params['mini']['notifyUrlRecharges'];

                $WeChatPayOne = new WeChatPayOne($appid, $openid, $mch_id,$key, $out_trade_no, $body, $total_fee, $notify_url, $spdill_create_ip);
                $result = $WeChatPayOne->pay();

            } else {
                //用户在特约商户号里充值
                //判断商户是否存在
                if (!array_key_exists($mid, Yii::$app->params['serviceNum'])) {
                    $jsonData = ResponseHelper::buildJsonData(400, '商家支付账号不存在,请联系商家或者开发者');
                    return $this->asJson($jsonData);
                }
                $appid = Yii::$app->params['serviceNum'][$mid]['appid']; //公众号appid
                $mch_id = Yii::$app->params['serviceNum']['mchId']; //服务商号
                $sub_mch_id = Yii::$app->params['serviceNum'][$mid]['sub_mch_id'];
                $sub_appid = Yii::$app->params['serviceNum'][$mid]['sub_appid']; //小程序appid
                $key = Yii::$app->params['serviceNum']['md5Key']; //支付密匙设置
                $body = '客户充值';
                $notify_url = Yii::$app->params['mini']['notifyUrlRecharge'];
                $WeiXinPay = new WeChatPay($appid, $openid, $mch_id, $key, $out_trade_no, $body, $total_fee, $notify_url, $spdill_create_ip, $sub_appid, $sub_mch_id);
                //发起微信支付
                $result = $WeiXinPay->pay();

            }

            if ($result['code'] == 0) {
                $jsonData = ResponseHelper::buildJsonData(400, '失败', $result['msg']);
                return $this->asJson($jsonData);
            } else {
                $recharge = [
                    'uid' => $uid,
                    'mid' => $mid,
                    'openid' => $openid,
                    'name' => $name,
                    'phone' => $phone,
                    'out_trade_no'=>$out_trade_no,
                    'unionid' => $unionid,
                    'recharge' => $total_fee,
                    'create_time' => time(),
                    'status' => -1,
                ];
                $rechargeGet = new Recharge();
                $res = $rechargeGet->rechargeInset($recharge);
            }

            if ($res) {
                $orderId = $rechargeGet->rechargeGetId($out_trade_no);
                $result['oid'] = $orderId['id'];
                $jsonData = ResponseHelper::buildJsonData(200, '成功', $result);
            } else {
                $jsonData = ResponseHelper::buildJsonData(400, '订单生成失败');
            }

            //根据回调给用户充值金额

        }

        return $this->asJson($jsonData);
    }
}